{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Clumsy Factorial"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #stack #math #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #栈 #数学 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: clumsy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #笨阶乘"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>通常，正整数 <code>n</code> 的阶乘是所有小于或等于 <code>n</code> 的正整数的乘积。例如，<code>factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1</code>。</p>\n",
    "\n",
    "<p>相反，我们设计了一个笨阶乘 <code>clumsy</code>：在整数的递减序列中，我们以一个固定顺序的操作符序列来依次替换原有的乘法操作符：乘法(*)，除法(/)，加法(+)和减法(-)。</p>\n",
    "\n",
    "<p>例如，<code>clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1</code>。然而，这些运算仍然使用通常的算术运算顺序：我们在任何加、减步骤之前执行所有的乘法和除法步骤，并且按从左到右处理乘法和除法步骤。</p>\n",
    "\n",
    "<p>另外，我们使用的除法是地板除法（<em>floor division</em>），所以&nbsp;<code>10 * 9 / 8</code>&nbsp;等于&nbsp;<code>11</code>。这保证结果是一个整数。</p>\n",
    "\n",
    "<p>实现上面定义的笨函数：给定一个整数 <code>N</code>，它返回 <code>N</code> 的笨阶乘。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>4\n",
    "<strong>输出：</strong>7\n",
    "<strong>解释：</strong>7 = 4 * 3 / 2 + 1\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>10\n",
    "<strong>输出：</strong>12\n",
    "<strong>解释：</strong>12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ol>\n",
    "\t<li><code>1 &lt;= N &lt;= 10000</code></li>\n",
    "\t<li><code>-2^31 &lt;= answer &lt;= 2^31 - 1</code>&nbsp; （答案保证符合 32 位整数。）</li>\n",
    "</ol>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [clumsy-factorial](https://leetcode.cn/problems/clumsy-factorial/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [clumsy-factorial](https://leetcode.cn/problems/clumsy-factorial/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['4', '10']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        if n < 3:\n",
    "            return n\n",
    "        if n == 3:\n",
    "            return 6\n",
    "        if n == 4:\n",
    "            return 7\n",
    "        if n % 4 == 0:\n",
    "            return n + 1\n",
    "        if n % 4 < 3:\n",
    "            return n + 2\n",
    "        return n - 1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        if N == 1:\n",
    "            return 1\n",
    "        elif N == 2:\n",
    "            return 2\n",
    "        elif N == 3:\n",
    "            return 6\n",
    "        \n",
    "        res = 0\n",
    "        cnt = N\n",
    "        op = 1\n",
    "        while(N >= 4):\n",
    "            res += int(math.floor(N * (N - 1) / (N -2)) * op)\n",
    "            res += N - 3\n",
    "            if op == 1:\n",
    "                op = -1\n",
    "            N -= 4\n",
    "        \n",
    "        \n",
    "        if N == 0:\n",
    "            return res\n",
    "        elif N == 1:\n",
    "            return res - 1\n",
    "        elif N == 2:# 6 *5 /4 +3-2*1\n",
    "            return res - 2\n",
    "        else: #7*6/5+4-3*2/1\n",
    "            return res - 6\n",
    "\n",
    "\n",
    "                 \n",
    "                \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        ans=0\n",
    "        if n>2:\n",
    "            ans+=n*(n-1)//(n-2)\n",
    "            ans+=n-3\n",
    "        elif n==2:\n",
    "            ans+=n*(n-1)\n",
    "        else:\n",
    "            ans+=1\n",
    "        for i in range(n-4,0,-4):\n",
    "            if i>2:\n",
    "                ans-=i*(i-1)//(i-2)\n",
    "                ans+=i-3\n",
    "            elif i==2:\n",
    "                ans-=i*(i-1)\n",
    "            else:\n",
    "                ans-=1\n",
    "        return (ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def clumsy(self, n: int) -> int:\r\n",
    "        stack = [n]\r\n",
    "        j = 0\r\n",
    "        for i in range(n - 1, 0, -1):\r\n",
    "            if j == 0:\r\n",
    "                stack.append(stack.pop() * i)\r\n",
    "            elif j == 1:\r\n",
    "                stack.append(int(stack.pop()/float(i)))\r\n",
    "            elif j == 2:\r\n",
    "                stack.append(i)\r\n",
    "            elif j == 3:\r\n",
    "                stack.append(-i)\r\n",
    "            j = (j + 1) % 4\r\n",
    "        return sum(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        stack = [n]\n",
    "        op = 0\n",
    "        for i in range(n - 1, 0, -1):\n",
    "            if op == 0: # *\n",
    "                stack.append(stack.pop() * i)\n",
    "            elif op == 1: # /\n",
    "                stack.append(int(stack.pop() / float(i)))\n",
    "            elif op == 2: # +\n",
    "                stack.append(i)\n",
    "            else: # -\n",
    "                stack.append(-i)\n",
    "            op = (op + 1) % 4\n",
    "        return sum(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        stack = deque()\n",
    "        stack.append(n)\n",
    "        index = 1\n",
    "        n -= 1\n",
    "        while (n):\n",
    "            if (index%4==1):\n",
    "                stack[-1]*=n\n",
    "            elif (index%4==2):\n",
    "                stack[-1] = int(stack[-1]/n)\n",
    "            elif (index%4==3):\n",
    "                stack.append(n)\n",
    "            elif (index%4==0):\n",
    "                stack.append(-n)\n",
    "            n -= 1\n",
    "            index += 1\n",
    "        return sum(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        stack = [n]\n",
    "        s = ['*','/','+','-']\n",
    "        idx = 0\n",
    "        for x in range(n-1,0,-1):\n",
    "            if idx == 0:\n",
    "                stack.append(stack.pop()*x)\n",
    "                idx += 1\n",
    "                continue\n",
    "            elif idx == 1:\n",
    "                stack.append(int(stack.pop()/x))\n",
    "                idx += 1\n",
    "                continue\n",
    "            elif idx == 2:\n",
    "                stack.append(x)\n",
    "                idx += 1\n",
    "                continue\n",
    "            elif idx == 3:\n",
    "                stack.append(-x)\n",
    "                idx = 0\n",
    "        ans = 0\n",
    "        # print(stack,idx)\n",
    "        # for y in stack:\n",
    "        #     ans += y\n",
    "        return sum(stack)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        nums = list(range(n,0,-1))\n",
    "        tmplist = []\n",
    "\n",
    "        for i in range(n):\n",
    "            if i%4==0:\n",
    "                fisrt = nums[i]\n",
    "            elif i%4 == 1:\n",
    "                fisrt = nums[i]*fisrt\n",
    "            elif i%4==2:\n",
    "                fisrt = fisrt//nums[i]\n",
    "            elif i%4==3:\n",
    "                tmplist.append(fisrt)\n",
    "                tmplist.append(nums[i])\n",
    "            \n",
    "            if i%4!=3 and i==n-1:\n",
    "                tmplist.append(fisrt)\n",
    "\n",
    "        ans = tmplist[0]\n",
    "        for i in range(1,len(tmplist)):\n",
    "            if i%2==1:\n",
    "                ans += tmplist[i]\n",
    "            else:\n",
    "                ans -= tmplist[i]\n",
    "        return ans\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        stack = [n]\n",
    "        s = ['*','/','+','-']\n",
    "        idx = 0\n",
    "        for x in range(n-1,0,-1):\n",
    "            if idx == 0:\n",
    "                stack.append(stack.pop()*x)\n",
    "                idx += 1\n",
    "                continue\n",
    "            elif idx == 1:\n",
    "                stack.append(int(stack.pop()/x))\n",
    "                idx += 1\n",
    "                continue\n",
    "            elif idx == 2:\n",
    "                stack.append(x)\n",
    "                idx += 1\n",
    "                continue\n",
    "            elif idx == 3:\n",
    "                stack.append(-x)\n",
    "                idx = 0\n",
    "        ans = 0\n",
    "        print(stack,idx)\n",
    "        for y in stack:\n",
    "            ans += y\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        # 处理方法：栈 计算顺序：乘除加\n",
    "        if n == 0 or n == 1:\n",
    "            return 1\n",
    "        if n == 2:\n",
    "            return 2\n",
    "        if n == 3:\n",
    "            return 6\n",
    "        if n == 4:\n",
    "            return 7\n",
    "        stak = []\n",
    "        for i in range(1,n+1):\n",
    "            stak.append(i)\n",
    "        \n",
    "        m1 = stak.pop()\n",
    "        m2 = stak.pop()\n",
    "        m3 = stak.pop()\n",
    "        m4 = stak.pop()\n",
    "        ans = (m1*m2)//m3 + m4\n",
    "        while stak:\n",
    "            if len(stak) >= 4:\n",
    "                m1 = stak.pop()\n",
    "                m2 = stak.pop()\n",
    "                m3 = stak.pop()\n",
    "                m4 = stak.pop()\n",
    "                ans_temp = (m1*m2)//m3 - m4\n",
    "                ans = ans - ans_temp\n",
    "            if len(stak) == 3:\n",
    "                m1 = stak.pop()\n",
    "                m2 = stak.pop()\n",
    "                m3 = stak.pop()\n",
    "                ans_temp = (m1*m2)//m3\n",
    "                ans = ans - ans_temp\n",
    "            if len(stak) == 2:\n",
    "                m1 = stak.pop()\n",
    "                m2 = stak.pop()\n",
    "                ans_temp = (m1*m2)\n",
    "                ans = ans - ans_temp\n",
    "            if len(stak) == 1:\n",
    "                m1 = stak.pop()\n",
    "                ans_temp = m1\n",
    "                ans = ans - ans_temp\n",
    "            \n",
    "        return ans\n",
    "\n",
    "            \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        stack = []\n",
    "        name = {'+':lambda x,y:x+y,\n",
    "        '-':lambda x,y:x-y,\n",
    "        '*':lambda x,y:x*y,\n",
    "        '/':lambda x,y:x//y,}\n",
    "        for i in range(n,0,-1):\n",
    "            \n",
    "            if (n-i)%4==2:\n",
    "                num,s = stack.pop()\n",
    "                stack.append((name[s](num,i),'+'))\n",
    "            elif (n-i)%4==1:\n",
    "                num,s = stack.pop()\n",
    "                stack.append((name[s](num,i),'/')) \n",
    "            elif (n-i)%4==0:\n",
    "                stack.append((i,'*'))\n",
    "            elif (n-i)%4==3:\n",
    "                stack.append((i,'-'))\n",
    "        out = 0\n",
    "        sign = '+'\n",
    "        # print(stack)\n",
    "        while stack:\n",
    "            # print(out,sign)\n",
    "            num,s = stack.pop(0)\n",
    "            out = name[sign](out,num)\n",
    "            sign = s\n",
    "        return out\n",
    "\n",
    "                \n",
    "                    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        if n < 2:\n",
    "            return n\n",
    "        \n",
    "        nlist = []\n",
    "        slist = []\n",
    "        sign = 0\n",
    "        count = n\n",
    "        n -= 1\n",
    "        while n :\n",
    "            if sign == 0:\n",
    "                count *= n\n",
    "                n -= 1\n",
    "                sign = 1\n",
    "                continue\n",
    "            if sign == 1:\n",
    "                count //= n\n",
    "                n -=1\n",
    "                sign = 2\n",
    "                nlist.append(count)\n",
    "                count = 0\n",
    "                slist.append(sign)\n",
    "                continue\n",
    "            if sign == 2:\n",
    "                count += n\n",
    "                n -= 1\n",
    "                sign = 3\n",
    "                nlist.append(count)\n",
    "                count = 0\n",
    "                slist.append(sign)\n",
    "                continue\n",
    "            if sign == 3:\n",
    "                if n -2 > 0:\n",
    "                    count = n*(n-1)//(n-2)\n",
    "                    n -= 3\n",
    "                    sign = 2\n",
    "                    nlist.append(count)\n",
    "                    count = 0\n",
    "                    slist.append(sign)\n",
    "                    continue\n",
    "                elif n-1 >0:\n",
    "                    count = n*(n-1)\n",
    "                    n -= 2\n",
    "                    sign = 1\n",
    "                    nlist.append(count)\n",
    "                    count = 0\n",
    "                    continue\n",
    "                else:\n",
    "                    count = n\n",
    "                    n -= 1\n",
    "                    sign = 0\n",
    "                    nlist.append(count)\n",
    "                    count = 0\n",
    "                    continue\n",
    "        print(nlist)\n",
    "        for i in range(len(nlist)):\n",
    "            if i == 0:\n",
    "                count += nlist[i]\n",
    "                continue\n",
    "            if slist[i-1] == 2:\n",
    "                count += nlist[i]\n",
    "            if slist[i-1] == 3:\n",
    "                count -= nlist[i]\n",
    "\n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def cal(self,n):\n",
    "        if n==0:\n",
    "            return 1\n",
    "        if n==1:\n",
    "            return 1\n",
    "        if n==2:\n",
    "            return 2-1\n",
    "        if n==3:\n",
    "            return 3-2*1\n",
    "        if n==4:\n",
    "            return 4-3*2//1\n",
    "        \n",
    "        return n-((n-1)*(n-2)//(n-3))+self.cal(n-4)\n",
    "\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        if n==0:\n",
    "            return 1\n",
    "        if n==1:\n",
    "            return 1\n",
    "        if n==2:\n",
    "            return 2\n",
    "        if n==3:\n",
    "            return 3*2//1\n",
    "        return n*(n-1)//(n-2)+self.cal(n-3)\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        ori =[]\n",
    "        fz =[]\n",
    "        res=0\n",
    "        for i in range(n):\n",
    "            ori.append(i+1)\n",
    "        ori = ori[::-1]\n",
    "        for i in range(0,len(ori),4):\n",
    "            fz.append(ori[i:i+4])\n",
    "        def jisuan(a,flag=1):\n",
    "            if len(a) ==4:\n",
    "                if(flag==1):\n",
    "                    return a[0]*a[1]//a[2]+a[3]\n",
    "                else:\n",
    "                    return -(a[0]*a[1]//a[2])+a[3]\n",
    "            if len(a) ==3:\n",
    "                if (flag == 1):\n",
    "                    return a[0]*a[1]//a[2]\n",
    "                else:\n",
    "                    return -(a[0] * a[1] // a[2])\n",
    "            if len(a) ==2:\n",
    "                if (flag == 1):\n",
    "                    return a[0]*a[1]\n",
    "                else:\n",
    "                    return -a[0] * a[1]\n",
    "            if len(a) ==1:\n",
    "                if (flag == 1):\n",
    "                    return a[0]\n",
    "                else:\n",
    "                    return -a[0]\n",
    "        for i,arr in enumerate(fz):\n",
    "            if(i==0):\n",
    "                res+=jisuan(arr,1)\n",
    "            else:\n",
    "                res+=jisuan(arr,0)\n",
    "        return res\n",
    "        \n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def domath(self,op, op1, op2):\n",
    "        if op == '*':\n",
    "            return op1 * op2\n",
    "        elif op == '/':\n",
    "            return math.floor(op1 / op2)\n",
    "        elif op == '+':\n",
    "            return op1 + op2\n",
    "        elif op == '-':\n",
    "            return op1 - op2\n",
    "        else:\n",
    "            return op1 ** op2\n",
    "\n",
    "    def calculate(self,s):\n",
    "        class Stack:\n",
    "            def __init__(self):\n",
    "                self.items = []\n",
    "\n",
    "            def isEmpty(self):\n",
    "                return self.items == []\n",
    "\n",
    "            def push(self, item):\n",
    "                self.items.append(item)\n",
    "\n",
    "            def pop(self):\n",
    "                return self.items.pop()\n",
    "\n",
    "            def peek(self):\n",
    "                return self.items[len(self.items) - 1]\n",
    "\n",
    "            def size(self):\n",
    "                return len(self.items)\n",
    "        # 请在此编写你的代码（可删除pass语句）\n",
    "        opStack = Stack()\n",
    "        numStack = Stack()\n",
    "        prec = {}\n",
    "        prec['^'] = 4\n",
    "        prec['*'] = 3\n",
    "        prec['/'] = 3\n",
    "        prec['+'] = 2\n",
    "        prec['-'] = 2\n",
    "        prec['('] = 1\n",
    "        # s = s.split()\n",
    "        for token in s:\n",
    "            if token.isdigit():\n",
    "                numStack.push(int(token))\n",
    "            elif token == '(':\n",
    "                opStack.push(token)\n",
    "            elif token == ')':\n",
    "                toptoken = opStack.pop()\n",
    "                while toptoken != '(':\n",
    "                    operand2 = numStack.pop()\n",
    "                    operand1 = numStack.pop()\n",
    "                    result = self.domath(toptoken, operand1, operand2)\n",
    "                    numStack.push(result)\n",
    "                    toptoken = opStack.pop()\n",
    "            else:\n",
    "                while (not opStack.isEmpty()) and (prec[opStack.peek()] >= prec[token]):\n",
    "                    op = opStack.pop()\n",
    "                    operand2 = numStack.pop()\n",
    "                    operand1 = numStack.pop()\n",
    "                    result = self.domath(op, operand1, operand2)\n",
    "                    numStack.push(result)\n",
    "                opStack.push(token)\n",
    "        while not opStack.isEmpty():\n",
    "            op = opStack.pop()\n",
    "            operand2 = numStack.pop()\n",
    "            operand1 = numStack.pop()\n",
    "            result = self.domath(op, operand1, operand2)\n",
    "            numStack.push(result)\n",
    "        return numStack.pop()\n",
    "\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        fh=0\n",
    "        st=[]\n",
    "        for i in range(n):\n",
    "            st.append(str(n - i))\n",
    "            if fh==0:\n",
    "                st.append('*')\n",
    "            elif fh==1:\n",
    "                st.append('/')\n",
    "            elif fh==2:\n",
    "                st.append('+')\n",
    "            elif fh==3:\n",
    "                st.append('-')\n",
    "            fh+=1\n",
    "            fh=fh%4\n",
    "        del(st[-1])\n",
    "\n",
    "        return int(self.calculate(st))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        four=['*','/','+','-']\n",
    "        equal=[]\n",
    "        j=0\n",
    "        for i in range(n,0,-1):\n",
    "            equal.append(i)\n",
    "            equal.append(four[j%4])\n",
    "            j+=1\n",
    "        equal.pop(-1)\n",
    "        stack=[equal.pop(0)]\n",
    "        while equal:\n",
    "            if equal[0]=='*':\n",
    "                stack[-1]=stack[-1]*equal[1]\n",
    "            elif equal[0]=='/':\n",
    "                stack[-1]=int(stack[-1]/float(equal[1]))\n",
    "            elif equal[0]=='+':\n",
    "                stack.append(equal[0])\n",
    "                stack.append(equal[1])\n",
    "            elif equal[0]=='-':\n",
    "                stack.append('+')\n",
    "                stack.append(-equal[1])\n",
    "            equal.pop(0)\n",
    "            equal.pop(0)\n",
    "        output=stack.pop(-1)\n",
    "        while stack:\n",
    "            if stack[-1]=='+':\n",
    "                # if len(stack)>2:\n",
    "                #     if stack[-3]=='+':output=output+stack[-2]\n",
    "                #     elif stack[-3]=='-':output=stack[-2]-output\n",
    "                # else:\n",
    "                output=output+stack[-2]\n",
    "            elif stack[-1]=='-':\n",
    "                # if len(stack)>2:\n",
    "                #     if stack[-3]=='+':\n",
    "                #         output=stack[-2]-output\n",
    "                #     elif stack[-3]=='-':\n",
    "                #         output=output+stack[-2]\n",
    "                # else:\n",
    "                output=stack[-2]-output\n",
    "                # output=stack[-2]-output\n",
    "            stack.pop(-1)\n",
    "            stack.pop(-1)\n",
    "        return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        def dummy4(N):\n",
    "            if N >= 4:\n",
    "                return - (N*(N-1)//(N-2))+N-3 + dummy4(N-4)\n",
    "            elif N == 3:\n",
    "                return - (N*(N-1)//(N-2))\n",
    "            elif N == 2:\n",
    "                return - N*(N-1)\n",
    "            elif N <= 1:\n",
    "                return - N\n",
    "\n",
    "        if N >= 3:\n",
    "            addup = 2*(N*(N-1)//(N-2))\n",
    "        elif N == 2:\n",
    "            addup = 2*(N*(N-1))\n",
    "        else:\n",
    "            addup = 2*N\n",
    "\n",
    "        return dummy4(N) + addup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int, multiplier=1) -> int:\n",
    "        if N <= 2:\n",
    "            return multiplier*N\n",
    "        if N == 3:\n",
    "            return multiplier*6\n",
    "        \n",
    "        return multiplier*int(N*(N-1)/(N-2))+(N-3)+self.clumsy(N-4, -1)\n",
    "        \n",
    "        \n",
    "        \n",
    "\n",
    "\n",
    "                \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        nums = []\n",
    "        ops = []\n",
    "        nums.append(n)\n",
    "        while n > 0:\n",
    "            n -= 1\n",
    "            if n > 0:\n",
    "                ops.append('*')\n",
    "                nums.append(n)\n",
    "            n -= 1\n",
    "            if n > 0:\n",
    "                ops.append('/')\n",
    "                nums.append(n)\n",
    "            n -= 1\n",
    "            if n > 0:\n",
    "                ops.append('+')\n",
    "                nums.append(n)\n",
    "            n -= 1\n",
    "            if n > 0:\n",
    "                ops.append('-')\n",
    "                nums.append(n)\n",
    "        cnt = 0\n",
    "        for idx, val in enumerate(ops):\n",
    "            if val == '*':\n",
    "                cnt += 1\n",
    "                num1 = nums.pop(idx-cnt+1)\n",
    "                num2 = nums.pop(idx-cnt+1)\n",
    "                nums.insert(idx-cnt+1, num1 * num2)\n",
    "            elif val == '/':\n",
    "                cnt += 1\n",
    "                num1 = nums.pop(idx-cnt+1)\n",
    "                num2 = nums.pop(idx-cnt+1)\n",
    "                nums.insert(idx-cnt+1, math.floor(num1 / num2))\n",
    "        ops = [i for i in ops if i in ['+', '-']]\n",
    "        cnt = 0\n",
    "        for idx, val in enumerate(ops):\n",
    "            if val == '+':\n",
    "                cnt += 1\n",
    "                num1 = nums.pop(idx - cnt + 1)\n",
    "                num2 = nums.pop(idx - cnt + 1)\n",
    "                nums.insert(idx - cnt + 1, num1 + num2)\n",
    "            elif val == '-':\n",
    "                cnt += 1\n",
    "                num1 = nums.pop(idx - cnt + 1)\n",
    "                num2 = nums.pop(idx - cnt + 1)\n",
    "                nums.insert(idx - cnt + 1, math.floor(num1 - num2))\n",
    "        return nums[0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        op=['$']\n",
    "        temp=[]\n",
    "        dic={'*':2,'/':2,'+':1,'-':1,'$':-1}\n",
    "        count=0\n",
    "        def get_op(count):\n",
    "            if count%4==0:return '*'\n",
    "            elif count%4==1:return '/'\n",
    "            elif count%4==2:return '+'\n",
    "            else:return '-'\n",
    "        for i in range(n,0,-1):\n",
    "            temp.append(i)\n",
    "            if i>1:\n",
    "                s=get_op(count)\n",
    "                while dic[s]<=dic[op[-1]]:\n",
    "                    temp.append(op.pop())\n",
    "                op.append(s)\n",
    "            count+=1\n",
    "        while dic[op[-1]]>0:\n",
    "            temp.append(op.pop())\n",
    "        temp=temp[::-1]\n",
    "        out=[]\n",
    "        while temp:\n",
    "            if temp[-1] in dic:\n",
    "                s=temp.pop()\n",
    "                right=out.pop()\n",
    "                left=out.pop()\n",
    "                if s=='+':out.append(left+right)\n",
    "                elif s=='-':out.append(left-right)\n",
    "                elif s=='*':out.append(left*right)\n",
    "                else:out.append(left//right)\n",
    "            else:\n",
    "                out.append(temp.pop())\n",
    "        \n",
    "        return out[-1]\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        def clu(n):\n",
    "            if n >= 5:\n",
    "                return clu(n-4) - (n*(n-1)//(n-2)) + n - 3\n",
    "            elif n == 4:\n",
    "                return -5\n",
    "            elif n == 3:\n",
    "                return -6\n",
    "            elif n == 2:\n",
    "                return -2\n",
    "            else:\n",
    "                return -1\n",
    "        if n == 2:\n",
    "            return 2\n",
    "        if n == 1:\n",
    "            return 1\n",
    "        return clu(n) + 2*(n*(n-1)//(n-2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        res=[]\n",
    "        a=[]\n",
    "        def helper(k,res,a):\n",
    "            if k==3:\n",
    "                res.append(6)\n",
    "            elif k==2:\n",
    "                res.append(2)\n",
    "            elif k==1:\n",
    "                res.append(1)\n",
    "            elif k==0:\n",
    "                res.append(0)\n",
    "            else:\n",
    "                res.append(k*(k-1)//(k-2))\n",
    "                a.append(k-3)\n",
    "                helper(k-4,res,a)\n",
    "        helper(N,res,a)\n",
    "        return 2*res[0]-sum(res)+sum(a)\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        res = \"\"\n",
    "        count = 0\n",
    "        for i in range(N,0,-1):\n",
    "            res += str(i)\n",
    "            if count % 4 == 0:\n",
    "                res += \"*\"\n",
    "            elif count % 4 == 1:\n",
    "                res += \"//\"\n",
    "            elif count % 4 == 2:\n",
    "                res += \"+\"\n",
    "            else:\n",
    "                res += \"-\"\n",
    "            count += 1\n",
    "        if res[-1] == \"/\":\n",
    "            return eval(res[:-2])\n",
    "        return eval(res[:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        ans = [0]*n\n",
    "        # 定义辅助列表\n",
    "        g = [0]*n\n",
    "        ans[0:4] = [1,2,6,7]\n",
    "        g[0:4] = [1,2,6,5]  # 按乘除减加顺序计算\n",
    "        for i in range(4,n):\n",
    "            g[i] = math.floor((i+1) * i / (i-1)) - (i-2) + g[i-4]\n",
    "            ans[i] = math.floor((i+1) * i / (i-1)) + (i-2) - g[i-4]\n",
    "        print(g)\n",
    "        print(ans)\n",
    "        return ans[n-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    dp = {}\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        if n == 1:\n",
    "            return 1\n",
    "        if n == 2:\n",
    "            return 2\n",
    "        if n == 3:\n",
    "            return 6\n",
    "        val = (n*(n-1))//(n-2)\n",
    "        bias = self.calc(n-3)\n",
    "        return val + bias\n",
    "    \n",
    "    def calc(self, n: int) -> int:\n",
    "        if n == 1:\n",
    "            return 1\n",
    "        if n == 2:\n",
    "            return 1\n",
    "        if n == 3:\n",
    "            return 1\n",
    "        if n == 4:\n",
    "            return -2\n",
    "        if not self.dp.get(n-4):\n",
    "            self.dp[n-4] = self.calc(n-4)\n",
    "        return n - (n-1)*(n-2)//(n-3) + self.dp[n-4]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        a=[\"*\",\"//\",\"+\",\"-\"]\n",
    "        string1=\"\"\n",
    "        x=0\n",
    "        y=0\n",
    "        for i in range(N):\n",
    "            \n",
    "            \n",
    "            string1+=str(N-x)+a[y]\n",
    "            x+=1\n",
    "            y+=1\n",
    "            if y==4:\n",
    "                y=0\n",
    "        if string1[-1]==\"/\":\n",
    "            string1=string1[:-2]\n",
    "        else:string1 = string1[:-1]\n",
    "        print(string1)\n",
    "\n",
    "\n",
    "        return eval(string1)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        if N==1:return 1\n",
    "        op=['*','/','+','-']\n",
    "\n",
    "        nums=[]\n",
    "        oper=[]\n",
    "\n",
    "        for i in range(N,0,-1):\n",
    "            nums.append(i)\n",
    "            if i==1:continue\n",
    "            cnt=N-i\n",
    "            sig=Operation(op[cnt%4])\n",
    "            while len(oper)>0 and sig<oper[-1]:\n",
    "                nums.append(oper.pop())\n",
    "            oper.append(sig)\n",
    "        while len(oper)>0:\n",
    "            nums.append(oper.pop())\n",
    "        \n",
    "        print(Operation('*')<Operation('-'))\n",
    "\n",
    "        stack=[]\n",
    "        for c in nums:\n",
    "            if type(c)==int:\n",
    "                stack.append(c)\n",
    "            else:\n",
    "                stack.append(c.f(stack.pop(),stack.pop()))\n",
    "                print(stack)\n",
    "        return stack[-1]\n",
    "    \n",
    "class Operation:\n",
    "    def __init__(self,name):\n",
    "        if name=='*':\n",
    "            self.name='*'\n",
    "            self.f=lambda x,y:x*y\n",
    "        elif name=='/':\n",
    "            self.name='/'\n",
    "            self.f=lambda x,y:y//x\n",
    "        elif name=='+':\n",
    "            self.name='+'\n",
    "            self.f=lambda x,y:x+y\n",
    "        else :\n",
    "            self.name='-'\n",
    "            self.f=lambda x,y:y-x\n",
    "        print(self.f)\n",
    "    \n",
    "    def __lt__(self,other):\n",
    "\n",
    "        if (self.name=='*'or self.name=='/')and(other.name=='+'or other.name=='-'):return False\n",
    "        else:return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, x: int) -> int:\n",
    "        # funcs = [\n",
    "        #     lambda x, y: x*y,\n",
    "        #     lambda x, y: x//y,\n",
    "        #     lambda x, y: x-y,\n",
    "        #     lambda x, y: x+y,\n",
    "        # ]\n",
    "        if x == 1: return 1\n",
    "        if x == 2: return 2\n",
    "        if x == 3: return 6\n",
    "        if x == 4: return 7\n",
    "        @lru_cache\n",
    "        def dp(x):\n",
    "            if x == 1: return -1\n",
    "            if x == 2: return -2\n",
    "            if x == 3: return -6\n",
    "            if x == 4: return -5\n",
    "            return  - (x * (x-1) // (x-2)) + (x-3) + dp(x-4)\n",
    "        # print(x, dp(x))\n",
    "        return dp(x) + 2 * (x * (x-1) // (x-2))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        if N==1:return 1\n",
    "        op=['*','/','+','-']\n",
    "\n",
    "        nums=[]\n",
    "        oper=[]\n",
    "\n",
    "        for i in range(N,0,-1):\n",
    "            nums.append(i)\n",
    "            if i==1:continue\n",
    "            cnt=N-i\n",
    "            sig=Operation(op[cnt%4])\n",
    "            while len(oper)>0 and sig<oper[-1]:\n",
    "                nums.append(oper.pop())\n",
    "            oper.append(sig)\n",
    "        while len(oper)>0:\n",
    "            nums.append(oper.pop())\n",
    "        \n",
    "        print(Operation('*')<Operation('-'))\n",
    "\n",
    "        stack=[]\n",
    "        for c in nums:\n",
    "            if type(c)==int:\n",
    "                stack.append(c)\n",
    "            else:\n",
    "                stack.append(c.f(stack.pop(),stack.pop()))\n",
    "                print(stack)\n",
    "        return stack[-1]\n",
    "    \n",
    "class Operation:\n",
    "    def __init__(self,name):\n",
    "        if name=='*':\n",
    "            self.name='*'\n",
    "            self.f=lambda x,y:x*y\n",
    "        elif name=='/':\n",
    "            self.name='/'\n",
    "            self.f=lambda x,y:y//x\n",
    "        elif name=='+':\n",
    "            self.name='+'\n",
    "            self.f=lambda x,y:x+y\n",
    "        else :\n",
    "            self.name='-'\n",
    "            self.f=lambda x,y:y-x\n",
    "        print(self.f)\n",
    "    \n",
    "    def __lt__(self,other):\n",
    "\n",
    "        if (self.name=='*'or self.name=='/')and(other.name=='+'or other.name=='-'):return False\n",
    "        else:return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    dp = {}\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        if n == 1:\n",
    "            return 1\n",
    "        if n == 2:\n",
    "            return 2\n",
    "        if n == 3:\n",
    "            return 6\n",
    "        val = (n*(n-1))//(n-2)\n",
    "        bias = Solution.calc(n-3)\n",
    "        return val + bias\n",
    "    \n",
    "    @classmethod\n",
    "    def calc(cls, n: int) -> int:\n",
    "        if n == 1:\n",
    "            return 1\n",
    "        if n == 2:\n",
    "            return 1\n",
    "        if n == 3:\n",
    "            return 1\n",
    "        if n == 4:\n",
    "            return -2\n",
    "        if not cls.dp.get(n-4):\n",
    "            cls.dp[n-4] = cls.calc(n-4)\n",
    "        return n - (n-1)*(n-2)//(n-3) + cls.dp[n-4]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "'''\n",
    "这道题与逆波兰表达式是同样的思路，才用栈的方法来实现计算\n",
    "唯一的差别是，当为+或者-的时候不进行计算，继续进行数值的插入和符号\n",
    "的插入，同时需要注意一些细节，比如当迭代到最后一个数字即为1\n",
    "的时候不用再插入字符串了\n",
    "'''\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        #初始化单调栈\n",
    "        stack = []\n",
    "        #初始化需要插入的符号\n",
    "        sign = ['*','/','+','-']\n",
    "        #初始化需要插入的符号的索引\n",
    "        i = 0\n",
    "        #开始迭代插入的数值\n",
    "        for j in range(N,0,-1):\n",
    "            #插入数值\n",
    "            stack.append(str(j))\n",
    "            #当索引不为0表示已经插入了一个数值和符号了，判断插入的符号是否为*或者/\n",
    "            if i != 0 and stack[-2] in ['*','/']:\n",
    "                #弹出最面上的3个字符并进行计算数值\n",
    "                val = int(eval(stack.pop(-3)+stack.pop(-2)+stack.pop(-1)))\n",
    "                #重新放入栈中\n",
    "                stack.append(str(val))\n",
    "            #判断是否为最后一个数字了\n",
    "            if j >1:\n",
    "                #将符号放入单调栈中\n",
    "                stack.append(sign[i%4])\n",
    "            #进行符号索引的迭代\n",
    "            i+=1\n",
    "        #返回最终序列的+-结果\n",
    "        return eval(''.join(stack))\n",
    "\n",
    "\n",
    "\n",
    "                \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        d = {0:0, 1:1, 2:2, 3:6, 4:7}\n",
    "        def fn(n):\n",
    "            if n in d:\n",
    "                return d[n]\n",
    "            res = n * (n - 1) // (n - 2) + (n - 3) + fn(n - 4) - (n-4)*max(1,n-5)//(max(1, n-6))*2\n",
    "            d[n] = res\n",
    "            return res\n",
    "        return fn(N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        res = []\n",
    "        def func(n):        \n",
    "            if n <= 2:\n",
    "                res.append(n)\n",
    "            elif n == 3:\n",
    "                res.append(6)\n",
    "            elif n == 4:\n",
    "                res.append(6)\n",
    "                res.append(1)\n",
    "            else:\n",
    "                res.append(n * (n - 1) // (n- 2))\n",
    "                res.append(n - 3)\n",
    "                func(n - 4)\n",
    "        func(n)\n",
    "        n = len(res)\n",
    "        flag = 1\n",
    "        s = res[0]\n",
    "        for i in range(1 , n):\n",
    "            s += flag * res[i]\n",
    "            flag *= -1\n",
    "        return s\n",
    "        \n",
    "        return func(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        stack=[]\n",
    "        cnt=0\n",
    "        op=['*','/','+','-']\n",
    "        for i in range(n,0,-1):\n",
    "            if len(stack)==0:\n",
    "                stack.append(i) \n",
    "            else:\n",
    "                if stack[-1]=='*':\n",
    "                    stack.pop()\n",
    "                    stack.append(stack.pop()*i)\n",
    "                elif stack[-1]=='/':\n",
    "                    stack.pop()\n",
    "                    stack.append(stack.pop()//i)\n",
    "                else:\n",
    "                    stack.append(i)\n",
    "            if i!=1:\n",
    "                stack.append(op[cnt%4])\n",
    "                cnt+=1\n",
    "\n",
    "        \n",
    "        return eval(''.join(map(str,stack)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def clumsy(self, N: int) -> int:\r\n",
    "        symbols = [\"*\", \"//\", \"+\", \"-\"]\r\n",
    "        ans = \"\"\r\n",
    "        for i in range(N - 1) :\r\n",
    "            ans += str(N - i) + symbols[i % 4]\r\n",
    "        ans += \"1\"\r\n",
    "        return eval(ans)\r\n",
    "\r\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        单调栈 = [n]\n",
    "        符号集合 = '*/+-'\n",
    "        符号 = 0\n",
    "        for a in range(n-1, 0, -1):\n",
    "            match 符号集合[符号]:\n",
    "                case '*':\n",
    "                    单调栈[-1] *= a\n",
    "                case '/':\n",
    "                    单调栈[-1] //= a\n",
    "                case _:\n",
    "                    单调栈[-1] = str(单调栈[-1])\n",
    "                    单调栈.append(符号集合[符号])\n",
    "                    单调栈.append(a)\n",
    "            符号 = (符号+1)%4\n",
    "        单调栈[-1] = str(单调栈[-1])\n",
    "        return eval(\"\".join(单调栈))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        单调栈 = [n]\n",
    "        符号集合 = '*/+-'\n",
    "        符号 = 0\n",
    "        for a in range(n-1, 0, -1):\n",
    "            match 符号集合[符号]:\n",
    "                case '*':\n",
    "                    单调栈[-1] *= a\n",
    "                case '/':\n",
    "                    单调栈[-1] //= a\n",
    "                case _:\n",
    "                    单调栈[-1] = str(单调栈[-1])\n",
    "                    单调栈.append(符号集合[符号])\n",
    "                    单调栈.append(a)\n",
    "            符号 = (符号+1)%4\n",
    "        单调栈[-1] = str(单调栈[-1])\n",
    "        return eval(\"\".join(单调栈))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        单调栈 = [n]\n",
    "        符号集合 = '*/+-'\n",
    "        符号 = 0\n",
    "        for a in range(n-1, 0, -1):\n",
    "            match 符号集合[符号]:\n",
    "                case '*':\n",
    "                    单调栈[-1] *= a\n",
    "                case '/':\n",
    "                    单调栈[-1] //= a\n",
    "                case _:\n",
    "                    单调栈[-1] = str(单调栈[-1])\n",
    "                    单调栈.append(符号集合[符号])\n",
    "                    单调栈.append(a)\n",
    "            符号 = (符号+1)%4\n",
    "        单调栈[-1] = str(单调栈[-1])\n",
    "        return eval(\"\".join(单调栈))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self,N):\n",
    "        a = [0,1,2,6,7]\n",
    "        if N <= 4:\n",
    "            return a[N]\n",
    "        # N > 4\n",
    "        result = self.planA(N)\n",
    "        N = N - 4\n",
    "        return self.planB(N, result)\n",
    "    def planA(self,N):\n",
    "        result = N*(N-1)//(N-2)+(N-3)\n",
    "        return result\n",
    "    def planB(self,N,result):\n",
    "        b = [0,1,2,6,5]\n",
    "        if N <= 4:\n",
    "            return result - b[N]\n",
    "        result -= N*(N-1)//(N-2)-(N-3)\n",
    "        return self.planB(N-4, result)\n",
    "\n",
    "            \n",
    "\n",
    "            \n",
    "        \n",
    "        \n",
    "        \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        nums = [str(i+1) for i in range(N)][::-1]\n",
    "        exe = ['-', '+', '/', '*']\n",
    "        res_qua = []\n",
    "        for num in nums:\n",
    "            if exe:\n",
    "                res_qua += [num, exe[-1]]\n",
    "                exe.pop()\n",
    "            else:\n",
    "                exe = ['-', '+', '/', '*']\n",
    "                res_qua += [num, exe[-1]]\n",
    "                exe.pop()\n",
    "        res_qua = res_qua[:-1]\n",
    "        cal_stack = ''\n",
    "        res = ''\n",
    "        for i in res_qua:\n",
    "            if i not in ('+', '-'):\n",
    "                cal_stack = ''.join([cal_stack, i])\n",
    "            else:\n",
    "                tmp = int(eval(cal_stack))\n",
    "                cal_stack = ''\n",
    "                res = ''.join([res, str(tmp), i])\n",
    "        res = ''.join([res, str(int(eval(cal_stack)))])\n",
    "        return eval(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        def f(n, prefix):\n",
    "            if n == 0:\n",
    "                return 0\n",
    "            if n == 1:\n",
    "                return prefix * 1\n",
    "            if n == 2:\n",
    "                return prefix * 2\n",
    "            if n == 3:\n",
    "                return prefix * 6\n",
    "            return prefix * (n * (n-1) // (n-2)) + (n-3) + f(n-4, prefix=-1)\n",
    "        \n",
    "        return f(n, 1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, x: int) -> int:\n",
    "        def dfs(n: int, isback: bool) -> int:\n",
    "            if isback:\n",
    "                if n == 0:\n",
    "                    return 0\n",
    "                if n == 1:\n",
    "                    return -1\n",
    "                if n == 2:\n",
    "                    return -2 * 1\n",
    "                if n == 3:\n",
    "                    return -3 * 2 // 1\n",
    "            else:\n",
    "                if n == 0:\n",
    "                    return 0\n",
    "                if n == 1:\n",
    "                    return 1\n",
    "                if n == 2:\n",
    "                    return 2 * 1\n",
    "                if n == 3:\n",
    "                    return 3 * 2 // 1\n",
    "\n",
    "            if isback:\n",
    "                return -(n * (n - 1) // (n - 2)) + (n - 3) + dfs(n - 4, True)\n",
    "            else:\n",
    "                return n * (n - 1) // (n - 2) + (n - 3) + dfs(n - 4, True)\n",
    "        return dfs(x, False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        oper=(N//4+1)*[\"*\",\"//\",\"+\",\"-\"]\n",
    "        para=[str(i) for i in range(N,0,-1)]\n",
    "        return eval(\"\".join(map(lambda x,y:x+y,para,oper))[:-1].rstrip(\"/\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        return self.dfs(0,N-1,N)\n",
    "    \n",
    "    def dfs(self,u,cur,add):\n",
    "        if cur==0:\n",
    "            return add\n",
    "        val=u%4\n",
    "        if val==0:\n",
    "            if cur-1!=0:\n",
    "                add=add*cur//(cur-1)\n",
    "                cur=cur-1\n",
    "                u=u+1\n",
    "            else:\n",
    "                return add*cur\n",
    "        elif val==2:\n",
    "            add=add+cur\n",
    "        elif val==3:\n",
    "            if cur-1!=0:\n",
    "                if cur-2!=0:\n",
    "                    add=add-cur*(cur-1)//(cur-2)\n",
    "                    cur=cur-2\n",
    "                    u=u+2\n",
    "                else:\n",
    "                    add=add-cur*(cur-1)\n",
    "                    return add\n",
    "            else:\n",
    "                return add-cur\n",
    "        return self.dfs(u+1,cur-1,add)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        a=[]\n",
    "        for i in range (1,N+1):\n",
    "            #i=str(i)\n",
    "            a.append(i)\n",
    "            a.append([])\n",
    "        a.pop()\n",
    "        a=a[::-1]\n",
    "        fh=['*','/','+','-']\n",
    "        for i in range(len(a)):\n",
    "            if a[i] ==[]:\n",
    "                fp=fh.pop(0)\n",
    "                a[i]=fp\n",
    "                fh.append(fp)\n",
    "        for i in range(len(a)):\n",
    "            if a[i]=='/':\n",
    "                res=a[i-3]*a[i-1]\n",
    "                res=int(res/a[i+1])\n",
    "                a[i+1]=res\n",
    "                for i in range(i-3,i+1):\n",
    "                    a[i]=0\n",
    "        res1=[]\n",
    "        for i in range(len(a)):\n",
    "            a[i]=str(a[i])\n",
    "            if a[i]!='0':\n",
    "                res1.append(a[i])\n",
    "        res=''\n",
    "        for i in range(len(res1)):\n",
    "            res+=res1[i]\n",
    "        res=eval(res)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        return self.dfs(0,N-1,N)\n",
    "    \n",
    "    def dfs(self,u,cur,add):\n",
    "        if cur==0:\n",
    "            return add\n",
    "        val=u%4\n",
    "        if val==0:\n",
    "            if cur-1!=0:\n",
    "                add=add*cur//(cur-1)\n",
    "                cur=cur-1\n",
    "                u=u+1\n",
    "            else:\n",
    "                return add*cur\n",
    "        elif val==2:\n",
    "            add=add+cur\n",
    "        elif val==3:\n",
    "            if cur-1!=0:\n",
    "                if cur-2!=0:\n",
    "                    add=add-cur*(cur-1)//(cur-2)\n",
    "                    cur=cur-2\n",
    "                    u=u+2\n",
    "                else:\n",
    "                    add=add-cur*(cur-1)\n",
    "                    return add\n",
    "            else:\n",
    "                return add-cur\n",
    "        print(cur)\n",
    "        return self.dfs(u+1,cur-1,add)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        flatten = [*reversed(*[range(1, N+1)])]\n",
    "        shift_window = [flatten[4*n:(4*n)+4] for n in range(len(flatten)//4 + 1) ]\n",
    "        s = []\n",
    "        for nset in shift_window :\n",
    "            print(nset)\n",
    "            if nset :\n",
    "                cstr = '%i'%nset[0]\n",
    "                if len(nset) > 1 :\n",
    "                    cstr+= '*%i'%nset[1]\n",
    "                if len(nset) > 2 :\n",
    "                    cstr+= '//%i'%nset[2]\n",
    "                if len(nset) > 3 :\n",
    "                    cstr+= '+%i'%nset[3]\n",
    "                s.append(cstr)\n",
    "        calc = '-'.join(s)\n",
    "        return eval(calc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        if N == 1: return 1\n",
    "\n",
    "        def helper(cur_num, cur_ans, cur_op):\n",
    "            if cur_num == 0: return cur_ans\n",
    "            if cur_op == '*':\n",
    "                cur_ans *= cur_num\n",
    "                return helper(cur_num - 1, cur_ans, '/')\n",
    "            elif cur_op == '/':\n",
    "                cur_ans = cur_ans // cur_num\n",
    "                return helper(cur_num - 1, cur_ans, '+')\n",
    "            elif cur_op == '+':\n",
    "                cur_ans += cur_num\n",
    "                return helper(cur_num - 1, cur_ans, '-')\n",
    "            else:\n",
    "                if cur_num >= 3:\n",
    "                    cur_ans =  cur_ans - cur_num * (cur_num - 1) // (cur_num - 2)\n",
    "                    return helper(cur_num - 3, cur_ans, '+')\n",
    "                else:\n",
    "                    return cur_ans - cur_num\n",
    "                \n",
    "        return helper(N-1, N, '*')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        s = []\n",
    "        op = 0\n",
    "        num_2_op = {0:'*', 1:'/', 2:'+', 3:'-'}\n",
    "        for num in range(n, 0, -1):\n",
    "            s.append(num)\n",
    "            if num != 1:\n",
    "                s.append(num_2_op[op])\n",
    "                op = (op + 1) % 4\n",
    "\n",
    "        stack = []\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] not in ['/', '*']:\n",
    "                stack.append(s[i])\n",
    "                i += 1\n",
    "            else:\n",
    "                n1 = stack.pop(-1)\n",
    "                n2 = s[i+1]\n",
    "                if s[i] == '*':\n",
    "                    stack.append(n1*n2)\n",
    "                else:\n",
    "                    stack.append(n1//n2)\n",
    "                i+=2\n",
    "        stack = [str(item) for item in stack]\n",
    "        stack = ''.join(stack)\n",
    "        return eval(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        s = ''\n",
    "        d = {0 : '*', 1 : '/', 2 : '+', 3 : '-'}\n",
    "        cnt = 0\n",
    "        for i in range(N, 0, -1):\n",
    "            s += str(i) \n",
    "            s += d[cnt]\n",
    "            cnt += 1\n",
    "            cnt %= 4\n",
    "        s = s[:len(s)-1]\n",
    "        s = s.replace('/', '//')\n",
    "        return eval(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        cal_str = \"\"\n",
    "        sysbols_count = 0\n",
    "        sysbols = ['*', '//', '+', '-']\n",
    "        for i in range(N, 0, -1):\n",
    "            if i > 1:\n",
    "                cal_str += str(i) + sysbols[sysbols_count % 4]\n",
    "                sysbols_count += 1\n",
    "            else:\n",
    "                cal_str += str(i)\n",
    "        res = int(eval(cal_str))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        a=0\n",
    "        result=str(N)\n",
    "        for number in range (N-1,0,-1):\n",
    "            a+=1\n",
    "            if a%4==1:\n",
    "                result+=\"*\"+str(number)\n",
    "            elif a%4==2:\n",
    "                result+=\"//\"+str(number)\n",
    "            elif a%4==3:\n",
    "                result+=\"+\"+str(number)\n",
    "            else:\n",
    "                result+=\"-\"+str(number)\n",
    "        return(eval(result))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        string=''\n",
    "        for i in  range(N,0,-1):\n",
    "            string+=str(i)+'*'\n",
    "        string=list(string[:-1])\n",
    "        num=string.count('*')\n",
    "        stack=['*','//','+','-']\n",
    "        for b in range(len(string)):\n",
    "            if not string[b].isdigit():\n",
    "                x=stack.pop(0)\n",
    "                string[b]=x\n",
    "                stack.append(x)\n",
    "        string=''.join(string)\n",
    "        return int(eval(string))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        std,res=0,''\n",
    "        for i in range(n,0,-1):\n",
    "            res+=str(i)\n",
    "            if i==1: break\n",
    "            if std%4==0:\n",
    "                res+='*'\n",
    "            elif std%4==1:\n",
    "                res+='//'\n",
    "            elif std%4==2:\n",
    "                res+='+'\n",
    "            else:\n",
    "                res+='-'\n",
    "            std+=1\n",
    "        return eval(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        from math import floor\n",
    "        symDict={0:'*',1:'/',2:'+',3:'-'}\n",
    "        std=0\n",
    "        res=''\n",
    "        for i in range(n,0,-1):\n",
    "            res+=str(i)\n",
    "            if i==1: break\n",
    "            if std%4==0:\n",
    "                res+='*'\n",
    "            elif std%4==1:\n",
    "                res+='//'\n",
    "            elif std%4==2:\n",
    "                res+='+'\n",
    "            else:\n",
    "                res+='-'\n",
    "            std+=1\n",
    "        return eval(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        opers=['*','//','+','-']\n",
    "        string,q='',0\n",
    "        for i in range(n,0,-1):\n",
    "            if i==1:\n",
    "                string+=str(1)\n",
    "                break\n",
    "            string+=str(i)+opers[q%4]\n",
    "            q+=1\n",
    "        return eval(string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        opers=['*','//','+','-']\n",
    "        string,q='',0\n",
    "        for i in range(n,0,-1):\n",
    "            if i==1:\n",
    "                string+=str(1)\n",
    "                break\n",
    "            string+=str(i)+opers[q%4]\n",
    "            q+=1\n",
    "        return eval(string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def clumsy(self, n: int) -> int:\r\n",
    "        op = ['*','//','+','-']\r\n",
    "        o = 0\r\n",
    "        exp = \"\"\r\n",
    "        for i in range(n,0,-1):\r\n",
    "            exp += str(i)\r\n",
    "            if i!=1:\r\n",
    "                exp += op[o]\r\n",
    "            o = (o+1)%4\r\n",
    "        return eval(exp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        s = [\"*\", \"//\", \"+\", \"-\"]\n",
    "        res = \"\"\n",
    "        flag = 0\n",
    "        for i in range(n, 0, -1):\n",
    "            res += str(i)\n",
    "            if i != 1:\n",
    "                res += s[flag % 4]\n",
    "                flag += 1\n",
    "        return eval(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        s = str(n)\n",
    "        sign = [\"*\",\"//\",\"+\",\"-\"]\n",
    "        for i in range(n-1):\n",
    "            s += sign[i % 4] + str(n - i - 1)\n",
    "        \n",
    "        return eval(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        oper = ['*', '//', '+', '-']\n",
    "        s = str(n)\n",
    "        count = 0\n",
    "        while n >= 2:\n",
    "            n -= 1\n",
    "            s += oper[count]\n",
    "            s += str(n)\n",
    "            count += 1\n",
    "            count %= 4\n",
    "        return eval(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        res = f'{n}'\n",
    "        syms = ['*','//','+','-']\n",
    "        l = 0\n",
    "        for i in range(n - 1, 0, -1):\n",
    "            res += syms[l%4] + str(i)\n",
    "            l += 1\n",
    "        return eval(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        res = f'{n}'\n",
    "        syms = ['*','//','+','-']\n",
    "        l = 0\n",
    "        for i in range(n - 1, 0, -1):\n",
    "            res += syms[l%4] + str(i)\n",
    "            l += 1\n",
    "        return eval(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        s = ''.join(list(chain(*zip([str(i) for i in range(n, 0, -1)], [['*','//','+','-'][i % 4] for i in range(n)]))))[:-1]\n",
    "        if s[-1] == '/':\n",
    "            s = s[:-1]\n",
    "        return eval(s)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        factorial_list = []\n",
    "        for i in range(1, n + 1):\n",
    "            factorial_list.append(i)\n",
    "        factorial_list = factorial_list[::-1]\n",
    "        operator_list = [\"*\", \"//\", \"+\", \"-\"]\n",
    "        count = 0\n",
    "        result = str(factorial_list[0])\n",
    "        for i in range(1, len(factorial_list)):\n",
    "            if count == 4:\n",
    "                count = 0\n",
    "            result += operator_list[count] + str(factorial_list[i])\n",
    "            count += 1\n",
    "        return eval(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        ops = ['*', '//', '+', '-']\n",
    "        stk = []\n",
    "        k = 0\n",
    "        for i in range(n,0,-1):\n",
    "            stk.append(str(i))\n",
    "            stk.append(ops[k%4])\n",
    "            k += 1\n",
    "        # print(stk)\n",
    "        return eval(''.join(stk[:-1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, N: int) -> int:\n",
    "        star=[\"*\",\"//\",\"+\",\"-\"]\n",
    "        jk='N'\n",
    "        j = 0\n",
    "        for i in range(N-1,0,-1): \n",
    "                jk =jk+ star[j] + str(i)\n",
    "                if j< 3:\n",
    "                    j+=1\n",
    "                else:\n",
    "                    j=0\n",
    "\n",
    "        return eval(jk)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        # cnt=0\n",
    "        # res=n\n",
    "        # ans=[]\n",
    "\n",
    "        # for i in range(n-1,0,-1):\n",
    "        #     if cnt%4==0:\n",
    "        #         res*=i\n",
    "        #     elif cnt%4==1:\n",
    "        #         res=res//i\n",
    "        #     elif cnt%4==2:\n",
    "        #         res+=i\n",
    "        #         temp=i\n",
    "        #     else:\n",
    "        #         if cnt==3:\n",
    "        #             ans.append(res)\n",
    "        #         if cnt>6:\n",
    "        #             ans.append(res-temp)\n",
    "        #             ans.append(temp)\n",
    "        #         res=i\n",
    "        #         temp=0\n",
    "        #     # print(res,cnt,ans)\n",
    "        #     cnt+=1\n",
    "        # if cnt>3 and cnt%4!=3:\n",
    "        #     ans.append(res)\n",
    "        # if cnt>3 and cnt%4==3:\n",
    "        #     ans.append(res-temp)\n",
    "        #     ans.append(temp)\n",
    "        # # print(res,ans)\n",
    "\n",
    "        # if ans==[]:\n",
    "        #     return res\n",
    "        # res=ans[0]\n",
    "        # for i in range(1,len(ans)):\n",
    "        #     if i%2==1:\n",
    "        #         res-=ans[i]\n",
    "        #     else:\n",
    "        #         res+=ans[i]\n",
    "        # return res\n",
    "\n",
    "        s=''\n",
    "        l=['*', '//', '+', '-']\n",
    "        nums = list(range(1, n + 1))\n",
    "        nums.reverse()\n",
    "        for i in range(n):\n",
    "            s += str(nums[i]) + l[i % 4]\n",
    "        t=''\n",
    "        if s[-1] == '/':\n",
    "            t = s[:-2]\n",
    "        else:\n",
    "            t = s[:-1]\n",
    "        return eval(t)\n",
    "\n",
    "\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        s = \"\"\n",
    "        l = [\"*\", \"//\", \"+\", \"-\"]\n",
    "        nums = list(range(n, 0, -1))\n",
    "        for i in range(len(nums)):\n",
    "            s += str(nums[i]) + l[i % 4]\n",
    "        t = \"\"\n",
    "        if s[-1] == \"/\":\n",
    "            t = s[:-2]\n",
    "        else:\n",
    "            t = s[:-1]\n",
    "        return eval(t)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        list0=['*','//','+','-']\n",
    "        listnum=[i for i in range(n,0,-1)]\n",
    "        s=''\n",
    "        for No,num in enumerate(listnum[:-1],0):\n",
    "            no=No%4\n",
    "            s+=str(num)+list0[no]\n",
    "        s+=str(listnum[-1])\n",
    "        return eval(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        list0=['*','//','+','-']\n",
    "        listnum=[i for i in range(n,0,-1)]\n",
    "        s=''\n",
    "        for No,num in enumerate(listnum[:-1],0):\n",
    "            no=No%4\n",
    "            s+=str(num)+list0[no]\n",
    "        s+=str(listnum[-1])\n",
    "        return eval(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        ops = ['*', '//', '+', '-']\n",
    "        ans = ''\n",
    "        for i in range(n,1, -1):\n",
    "            ans += str(i)+ops[(n-i)%4]\n",
    "        ans += \"1\"\n",
    "        return eval(ans)\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def clumsy(self, n: int) -> int:\n",
    "        ans = str(n)\n",
    "        for i in range(1, n):\n",
    "            if i%4==1:\n",
    "                ans += '*' + str(n-i)\n",
    "            if i%4==2:\n",
    "                ans += '//' + str(n-i)\n",
    "            if i%4==3:\n",
    "                ans += '+' + str(n-i)\n",
    "            if i%4==0:\n",
    "                ans += '-' + str(n-i)\n",
    "        return eval(ans)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
